from sqlalchemy import Column, BigInteger, DateTime, Index
from app.config.database import Base
from datetime import datetime
from pydantic import BaseModel, ConfigDict
from typing import Optional

class RolePermission(Base):
    __tablename__ = "sys_role_permission"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment="关联ID")
    role_id = Column(BigInteger, nullable=False, comment="角色ID")
    permission_id = Column(BigInteger, nullable=False, comment="权限ID")
    create_time = Column(DateTime, default=datetime.utcnow, nullable=False, comment="创建时间")
    
    # 索引
    __table_args__ = (
        Index('uk_role_permission', 'role_id', 'permission_id'),
        Index('idx_role_id', 'role_id'),
        Index('idx_permission_id', 'permission_id')
    )

class RolePermissionInDB(BaseModel):
    id: int
    role_id: int
    permission_id: int
    create_time: Optional[datetime] = None

    model_config = ConfigDict(from_attributes=True)